﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.DO;
using SES.CMS.BL;
using SES.CMS.AdminCP;
using System.Drawing.Imaging;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;


namespace SES.CMS.AdminCP
{
    public partial class AddMultiImages : System.Web.UI.Page
    {
        MediaDO objMedia = new MediaDO();

        protected void Page_Load(object sender, EventArgs e)
        {
            //  Functions.ddlDatabinder(cboAlbum, "AlbumID", "Title", new cmsAlbumBL().SelectAll());
            if (!IsPostBack)
            {
                Functions.ddlDatabinder(ddlProduct, cmsProductDO.PRODUCTID_FIELD, cmsProductDO.PRODUCTTITLE_FIELD, new cmsProductBL().SelectAll());
                Functions.ddlDatabinder(ddlAlbum, cmsAlbumDO.ALBUMID_FIELD, cmsAlbumDO.TITLE_FIELD, new cmsAlbumBL().GetByType(true));
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            initObject(fuImage1, txtTitle1.Text, 1);
            initObject(fuImage2, txtTitle2.Text, 2);
            initObject(fuImage3, txtTitle3.Text, 3);
            initObject(fuImage4, txtTitle4.Text, 4);
            initObject(fuImage5, txtTitle5.Text, 5);
            Functions.Alert("Cập nhật thành công!", "/AdminCP/ListImages.aspx");
        }

        private void initObject(FileUpload fulImage, string title, int number)
        {
            if (string.IsNullOrEmpty(title))
                objMedia.Title = "Không có tiêu đề - " + number;
            else
                objMedia.Title = title;
            objMedia.AlbumID = int.Parse(ddlAlbum.SelectedValue);
            objMedia.MediaType = true;
            objMedia.ProductID = int.Parse(ddlProduct.SelectedValue);
            objMedia.OrderID = 0;
            objMedia.DateUpload = DateTime.Now;
            if (!string.IsNullOrEmpty(fulImage.FileName))
            {
                objMedia.MediaFile = UploadFile(fulImage, objMedia.Title);
                new MediaBL().Insert(objMedia);
            }
        }

        private string UploadFile(FileUpload fulImages, string title)
        {
            if (!string.IsNullOrEmpty(fulImages.FileName))
            {
                string FileName = string.Format("{0}{1}", Functions.Change_AV(title) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss"), fulImages.FileName.Substring(fulImages.FileName.LastIndexOf(".")));

                string preName = Functions.Change_AV(title) + "-" + DateTime.Now.ToString("ddMMyyyyhhmmss");
                string typeName = fulImages.FileName.Substring(fulImages.FileName.LastIndexOf("."));

                string SaveLocation = string.Format("{0}\\{1}", Server.MapPath("~/Media/"), FileName);
                fulImages.SaveAs(SaveLocation);

                objMedia.Thumbnail = preName + "_thumbnail" + typeName;

                string pathAvatars = Server.MapPath("~/Media/thumbnail/");
                try
                {
                    int thumbWidth = 400;
                    int thumbHeight = 300;
                    System.Drawing.Image oImg = System.Drawing.Image.FromStream(fulImages.PostedFile.InputStream);
                    System.Drawing.Image oThumbNail = new Bitmap(thumbWidth, thumbHeight, oImg.PixelFormat);
                    Graphics oGraphic = Graphics.FromImage(oThumbNail);
                    oGraphic.CompositingQuality = CompositingQuality.HighQuality;
                    oGraphic.SmoothingMode = SmoothingMode.HighQuality;
                    oGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic;
                    Rectangle oRectangle = new Rectangle(0, 0, thumbWidth, thumbHeight);
                    oGraphic.DrawImage(oImg, oRectangle);
                    oThumbNail.Save(pathAvatars + objMedia.Thumbnail, ImageFormat.Jpeg);
                    oImg.Dispose();
                }
                catch (Exception ex)
                {
                    Response.Write("Error: " + ex.Message);
                }

                //string FilePreWatermark = Server.MapPath(@"~/Media/") + FileName;
                /////Key of Open file
                //FileStream fs = new FileStream(FilePreWatermark, FileMode.Open);
                //Bitmap bmp = new Bitmap(fs);
                //fs.Close();
                /////ohshit
                //Graphics canvas = Graphics.FromImage(bmp);
                //Bitmap bmpNew = new Bitmap(bmp.Width, bmp.Height);
                //canvas = Graphics.FromImage(bmpNew);
                //canvas.DrawImage(bmp, new Rectangle(0, 0, bmpNew.Width, bmpNew.Height), 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel);
                //bmp = bmpNew;

                //canvas.DrawString("www.topbike.vn", new Font("Arial", 14, FontStyle.Italic), new SolidBrush(Color.Red), (bmp.Width - 190), (bmp.Height - 25));

                //string NewFileName = Server.MapPath(@"~/Media/") + FileName;

                //bmp.Save(NewFileName, System.Drawing.Imaging.ImageFormat.Jpeg);

                //bmp.Dispose();
                //bmpNew.Dispose();
                
                return FileName;
            }
            return string.Empty;
        }
        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtTitle1.Text = "";
            txtTitle2.Text = "";
            txtTitle3.Text = "";
            txtTitle4.Text = "";
            txtTitle5.Text = "";
            txtTitle1.Focus();
            ddlProduct.SelectedValue = "0";
        }
    }
}